package com.ylfstu.smallchange.oop;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 该类完成零钱通各个功能
 * 使用OOP
 * 将各个功能对应一个方法
 */
public class SmallChangeSysOOP {
    boolean loop = true;
    Scanner scanner = new Scanner(System.in);
    String key = "";
    //2.完成零钱通明细
    String details = "----------------零钱通明细----------------";
    //3.完成收益入账
    double money = 0;
    double balance = 0;
    Date date = null;//date 是 java.util.Date 类型，表示日期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//用于日期格式化
    //4.消费
    String note = "";

    public void mainMenu(){
        do{
            System.out.println("\n================零钱通菜单(OOP)================");
            System.out.println("\t\t\t\t1 零钱通明细");
            System.out.println("\t\t\t\t2 收益入账");
            System.out.println("\t\t\t\t3 消   费");
            System.out.println("\t\t\t\t4 退   出");
            System.out.println("请选择(1~4):   ");
            key = scanner.next();
            switch(key){
                case "1":
                    this.detail();
                    break;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.pay();
                    break;
                case "4":
                    this.exit();
                    break;
                default:
                    System.out.println("选择有误，请重新选择！");
            }
        }while(loop);
    }
    //零钱通明细
    public void detail(){
        System.out.println(details);
    }

    public void income(){
        System.out.println("收益入账金额:");
        money = scanner.nextDouble();
        //校验money的值
        if (money <= 0) {
            System.out.println("收益入账金额 需要 大于0");
        }

        balance += money;
        //拼接收益入账信息到 details
        date = new Date();
        details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
    }

    public void pay(){
        System.out.println("消费金额:");
        money = scanner.nextDouble();
        //校验money
        if (money <= 0 || money >= balance) {
            System.out.println("消费金额 需要在0-" + balance + "之间");
        }

        System.out.println("消费说明:");
        note = scanner.next();
        balance -= money;
        //拼接消费信息到details
        details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
    }

    public void exit(){
        //  建议一段代码完成一个小功能，高内聚，低耦合
        String choice = "";
        while(true){
            System.out.println("你确定要退出吗？ y/n");
            choice = scanner.next();
            if ("y".equals(choice) || "n".equals(choice)) {
                break;
            } else {
                System.out.println("请输入正确的字符    y/n");
            }
        }
        if (choice.equals("y")) {
            loop = false;
        }
    }
}










